<?php
/**
 * Created by PhpStorm.
 * User: Auser
 * Date: 2017/5/23
 * Time: 13:12
 */

namespace App\Services\Echarts;
use WechatUserService;
use UserRecordService;
use TestingConfig;
use ParamConfig;
use AdminEvaluateService;
use App\Helper\Tool;
use App\Admin\Controllers\DecreaseWeightController;
use Illuminate\Support\Facades\DB;
use Encore\Admin\Facades\Admin;
use App\Services\IdEncryptService;
use SendTplUserService;
use UserRemarkService;
use JzNutritionistService;
use App\Models\ChinaArea;
use App\Models\JzOrderDetail;
use App\Models\JzMatchRelation;
use JzOrderDetailService;
use JzMatchRelationService;
use Excel;
class DecreaseSelectHelper
{
    protected $tag = 'DW.select';

    /**从数据表中获取多选选项
     * @param $type
     * @return array
     */
    public function options($type){
        $options = array(""=>" ");
        $data =  JzOrderDetail::all()->pluck($type,$type)->toArray();
        //$options = empty($data) ? $options : array_unique($data);
        if($data){
            $options =   array_merge($options,$data);
        }
        return $options;
    }

    /**删除订单
     * @param $oid
     * @return mixed
     */
    public function deleteOrder($oid){
        //清除缓存
        $data= JzMatchRelationService::getByOid($oid);
        JzMatchRelationService::forgretmatchByUidTid($data['uid'],$data['tid']);
        JzMatchRelationService::forgretgetByOid($oid);
      return  JzOrderDetailService::deleteOrders($oid);
    }

    /**删除关联关系
     * @param $oid
     */
    public function deleteMatch($oid,$appid){
        return JzMatchRelationService::deleteMatch($oid,$appid);
    }

    /**根据id获取编辑订单详情
     * @param $oid
     */
    public function orderEditInfo($oid,$appid){
        $data =array();
        if($oid){
            $datas =   JzOrderDetailService::orderInfos($oid);
            if($datas){
                $data =  json_decode(json_encode($datas),TRUE);
                //将省份转换为id
                $province = str_replace('省','',$data['province']);
                $city = str_replace('市','',$data['city']);
                $data['province']=$this->getIdByName($province);
                $data['city']=$this->getIdByName($city);
                $data['district']=$this->getIdByName($data['district']);
                //将宝贝标题改为id
                //$title = json_decode(ParamConfig::getValue('JZ.admin.oder.user.good_title'),true);
                //$data['good_title'] = array_search($data['good_title'],$title);
            }
        }
        $data['appid'] = $appid;
        return $data;

    }

    /**根据id获取订单详情
     * @param $oid
     */
    public function orderInfo($oid,$appid){
        if($oid){
            $data =   JzOrderDetailService::orderInfos($oid);
            $status = JzMatchRelationService::getByOid($oid);
            $str = '<div style="font-size: 0;">
                <div style="width: 33%;padding: 10px 0;display: inline-block;font-size: 16px;">收货人姓名：'.$data['shouhuo_name'].'</div>
                <div style="width: 33%;padding: 10px 0;display: inline-block;font-size: 16px;">买家会员名：'.$data['buyer_name'].'</div>
                <div style="width: 33%;padding: 10px 0;display: inline-block;font-size: 16px;">买家性别：'.$data['buyer_sex'].'</div>
                <div style="width: 33%;padding: 10px 0;display: inline-block;font-size: 16px;">收货人省份：'.$data['province'].'</div>
                <div style="width: 33%;padding: 10px 0;display: inline-block;font-size: 16px;">城市：'.$data['city'].'</div>
                <div style="width: 33%;padding: 10px 0;display: inline-block;font-size: 16px;">区县：'.$data['district'].'</div>
                <div style="width: 33%;padding: 10px 0;display: inline-block;font-size: 16px;">地址：'.$data['shouhuo_address'].'</div>
                <div style="width: 33%;padding: 10px 0;display: inline-block;font-size: 16px;">联系手机：'.$data['phone'].'</div>
                <div style="width: 33%;padding: 10px 0;display: inline-block;font-size: 16px;">物流公司：'.$data['wuliu'].'</div>
                <div style="width: 33%;padding: 10px 0;display: inline-block;font-size: 16px;">物流单号：'.$data['wuliu_id'].'</div>
                <div style="width: 33%;padding: 10px 0;display: inline-block;font-size: 16px;">订单状态：'.$data['order_status'].'</div>
                <div style="width: 33%;padding: 10px 0;display: inline-block;font-size: 16px;">宝贝标题：'.$data['good_title'].'</div>
                <div style="width: 33%;padding: 10px 0;display: inline-block;font-size: 16px;">微信名称：'.$data['nick_name'].'</div>
                <div style="width: 33%;padding: 10px 0;display: inline-block;font-size: 16px;">接单时间：'.$data['order_time'].'</div>
                <div style="width: 33%;padding: 10px 0;display: inline-block;font-size: 16px;">负责人：'.$data['principal'].'</div>
                <div style="width: 33%;padding: 10px 0;display: inline-block;font-size: 16px;">序号：'.$data['number'].'</div>
                <div style="width: 33%;padding: 10px 0;display: inline-block;font-size: 16px;">开始时间：'.$data['start_time'].'</div>
                <div style="width: 33%;padding: 10px 0;display: inline-block;font-size: 16px;">订单来源：'.$data['from'].'</div>
                <div style="width: 100%;padding: 10px 0;font-size: 16px;">商品留言：'.$data['good_message'].'</div>
                <div style="width: 100%;padding: 10px 0;font-size: 16px;">订单留言：'.$data['order_message'].'</div>';
            if($status){
                $record = UserRecordService::getByUid($status['uid'],$status['tid']);
                $rid = "";
                if($record){
                    $rid = $record->id;
                }
                    $str .= '<div style="text-align: right; font-size: 16px;padding: 10px 0;">
                        <button class="btn btn-large" type="button">已匹配</button>
                        <a href="/admin/testing/decrease/add/index?appid='.$appid.'&id='.$oid.'&uid='.$status['uid'].'&rid='.$rid.'"><button class="btn btn-large  btn btn-info" type="button">编辑订单</button></a>
                  </div>
                </div>';
            }else{
                    $str .= '<div style="text-align: right; font-size: 16px;padding: 10px 0;">
                        <a href="/admin/testing/decrease/match/index/'.$appid.'/'.$oid.'/'.$data['tid'].'?nickname='.$data['nick_name'].' "><button class="btn btn-large  btn btn-info" type="button">去匹配</button></a>
                        <a href="/admin/testing/decrease/add/index?appid='.$appid.'&id='.$oid.'"><button class="btn btn-large  btn btn-info" type="button">编辑订单</button></a>
                  </div>
                </div>';
            }
            return $str;
        }

    }

    /**手动保存用户
     * @param $arr
     * @return mixed
     */
    public function storeOrder($arr){
        $arr['appid'] = empty($arr['appid']) ? "" : $arr['appid'];
        //$title = json_decode(ParamConfig::getValue('JZ.admin.oder.user.good_title'),true);
        //$arr['good_title'] = $title[$arr['tid']];
        $arr['province'] = $this->getNameById($arr['province']);
        $arr['city'] = isset($arr['city']) ? $this->getNameById($arr['city']) : "";
        $arr['district'] = isset($arr['district']) ? $this->getNameById($arr['district']) : "";
        unset($arr['_token']);
        foreach($arr as $k => $v){
            if(!$v){
                unset($arr[$k]);
            }
        }
        //清除缓存
        JzMatchRelationService::forgretUserByAppid($arr['appid']);
        JzMatchRelationService::forgetUserByTidAppid($arr['appid'],$arr['tid']);
        if(isset($arr['id'])){
            $data = $arr;
            unset($data['id']);
            $res = JzOrderDetailService::edit($data,$arr['id']);
            return $arr['id'];
        }else{
            return JzOrderDetailService::store($arr);
        }
    }


    /**根据id返回城市名字
     * @param $id
     * @return mixed
     */
    protected function getNameById($id){
      $data =   ChinaArea::select('name')->where('id',$id)->first();
        return $data['name'];
    }

    protected function getIdByName($name){
        $data =   ChinaArea::select('id')->where('name',$name)->first();
        return $data['id'];
    }

    /**
     * @param $filepath
     * @param $tid
     * @param $appid
     */
    public function importOrder($filepath,$tid,$appid){
            //清除缓存
            JzMatchRelationService::forgretUserByAppid($appid);
            JzMatchRelationService::forgetUserByTidAppid($appid,$tid);

            Excel::load($filepath, function($reader) use($appid,$tid){
                $allData = $reader->all();
                $data = $allData[0];
                $batchData = [];
                foreach($data as $d){
                    $obj['tid']=$tid;
                    $obj['appid']=$appid;
                    $obj['order_id']=$d['order_id'];
                    $obj['wuliu']=$d['logistics_company'];
                    $obj['wuliu_id']=$d['logistics_id'];
                    $obj['order_status']=$d['order_status'];
                    $obj['buyer_name']=$d['buyer_name'];
                    $obj['buyer_sex']=$d['buyer_sex'];
                    $obj['buyer_province']=$d['buyer_province'];
                    $obj['buyer_city']=$d['buyer_city'];
                    $obj['buyer_pay_way']=$d['buy_methods'];
                    $obj['good_sku']=$d['sku'];
                    $obj['shouhuo_name']=$d['consignee_name'];
                    $obj['province']=$d['consignee_province'];
                    $obj['city']=$d['consignee_city'];
                    $obj['district']=$d['consignee_district'];
                    $obj['shouhuo_address']=$d['consignee_address'];
                    $obj['phone']=$d['phone'];
                    $obj['good_title']=$d['good_name'];
                    $obj['good_message']=$d['good_message'];
                    $obj['order_message']=$d['order_message'];
                    $obj['principal']=$d['principal'];
                    $obj['from']=$d['from'];
                    $obj['number']=$d['no'];
                    $obj['nick_name']=$d['weixin_nickname'];
                    $obj['order_time']=empty($d['order_time']) ? $d['order_time'] : $d['order_time']->toDateTimeString();
                    $obj['start_time']=empty($d['start_time']) ? $d['start_time'] : $d['start_time']->toDateTimeString();
                    array_push($batchData,$obj);
                }
              return  JzOrderDetailService::batchInsertData($batchData);
            });

        }

    /**存储匹配信息
     * @param $data
     */
    public function matchOrder($data){
        $appid = base64_decode($data['appid']);
        //清除缓存
        JzMatchRelationService::forgretUserByAppid($appid);
        JzMatchRelationService::forgetUserByTidAppid($appid,$data['tid']);

        $match['uid']=$data['uid'];
        $match['oid']=$data['oid'];
        $match['tid']=$data['tid'];
        $match['appid']=$appid;
        $insertId =  JzMatchRelationService::storeMatch($match);
        return $insertId;
    }

    /**根据appid tid 获取匹配表中的oid
     * @param $appid
     * @param string $tid
     * @return mixed
     */
    public function filterUser($appid,$tid=""){
        if($tid){
          $oid =  JzMatchRelationService:: getUserByTidAppid($tid,$appid);
        }else{
            $oid =  JzMatchRelationService:: getUserByAppid($appid);
        }
        $oid = json_decode(json_encode($oid),true);
        if($oid){
            $oid = array_unique($oid);
        }
        return $oid;
    }

    /**返回未匹配的订单id array
     * @param $appid  string
     * @param $filterUser  array
     */
    public function disFilterUser($appid,$filterUser){
        $oid = [];
        $idArr = JzOrderDetailService::getIdByAppid($appid);
        if($idArr){
            $idArr = json_decode(json_encode($idArr),true);
            if(count($idArr)>0 && count($idArr) >= count($filterUser)){
                $oid = array_diff($idArr,$filterUser);
            }
        }
        return $oid;
    }


    public function getNutritionist(){
        $result = JzNutritionistService::allName();
        $default = [ 0 => "请选择"];
        if($result){
            $nutritionist = json_decode(json_encode($result),true);
            $data = Array_merge($default,$nutritionist);
        }else{
            $data = $default;
        }
        return $data;
    }
















}